package com.wdh.testq.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;


import com.wdh.testq.R;

import java.text.SimpleDateFormat;
import java.util.Date;



/**
 * Created by Gss on 2019/3/25.
 */
public abstract class BaseDialog implements View.OnClickListener, CustomDialog.BackPressListener {
    protected Context mContext;
    protected CustomDialog mDialog;
    protected Display display;
    private float rate = 0f;
    private View rootView;
    protected String startTime;// 页面进入时间	 时间格式 yyyy-MM-dd HH:mm:ss
    protected String endTime;//页面退出时间  时间格式 yyyy-MM-dd HH:mm:ss

    public BaseDialog(Context mContext) {
        this.mContext = mContext;
        WindowManager mManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        display = mManager.getDefaultDisplay();
    }

    public <T extends BaseDialog> T setRate(float rate) {
        this.rate = rate;
        return (T) this;
    }

    public <T extends BaseDialog> T Build() {
        rootView = LayoutInflater.from(mContext).inflate(getResource(), null);
        mDialog = new CustomDialog(mContext, getDialogStyle());
        initView();
        initListener();
        mDialog.setContentView(rootView);
        mDialog.setBackPressListener(this);
        mDialog.setCanceledOnTouchOutside(getCanceledOnTouchOutside());
        mDialog.setEditFocusHideKeybord(getEditFocusHide());
        if (getAnimateStyle() != 0)
            mDialog.getWindow().setWindowAnimations(getAnimateStyle());
        if (rate == 0f) {
            mDialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
//            rootView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
        } else {
            mDialog.getWindow().setLayout((int) (display.getWidth() * rate), getDialogHeight());
//            rootView.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth() * rate), getDialogHeight()));
        }
        setCancelable(getCancelable());
        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                endTime = sdfDate.format(new Date(System.currentTimeMillis()));
                if (hideListener != null)
                    hideListener.hide();
            }
        });
        mDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                if (showListener != null)
                    showListener.show();
            }
        });
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        startTime = sdfDate.format(new Date(System.currentTimeMillis()));
        return (T) this;
    }

    protected boolean getCanceledOnTouchOutside() {
        return true;
    }

    protected int getAnimateStyle() {
        return 0;
    }

    protected boolean getEditFocusHide() {
        return true;
    }

    protected int getDialogStyle() {
        return R.style.AlertDialogStyle;
    }

    protected int getDialogHeight() {
        return FrameLayout.LayoutParams.WRAP_CONTENT;
    }

    public void hideDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    public Context getContext() {
        return mContext;
    }

    protected abstract void initListener();

    protected boolean getCancelable() {
        return true;
    }

    protected abstract void initView();

    protected void setCancelable(boolean b) {
        mDialog.setCancelable(b);
    }

    public void addSetOnClickListener(View v) {
        v.setOnClickListener(this);
    }

    public void show() {
        if (mContext instanceof Activity) {
            if (!((Activity) mContext).isFinishing())
                mDialog.show();
        }
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }

    protected abstract void doOnClick(View v);

    protected abstract int getResource();

    public void showMsg(String msg) {
        Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        doOnClick(view);
    }

    @Override
    public void onBackPress() {
        if (getBackPressHide() || getCancelable())
            hideDialog();
    }

    protected boolean getBackPressHide() {
        return true;
    }

    public View getRootView() {
        return rootView;
    }

    protected OnViewClickListener vClickListener;

    public <T extends BaseDialog> T setOnViewClickListener(OnViewClickListener l) {
        vClickListener = l;
        return (T) this;
    }

    public interface OnViewClickListener {
        void onClick(View view);
    }

    protected OnDialogHideListener hideListener;

    public <T extends BaseDialog> T setDialogHideListener(OnDialogHideListener l) {
        hideListener = l;
        return (T) this;
    }

    protected OnDialogShowListener showListener;

    public <T extends BaseDialog> T setDialogShowListener(OnDialogShowListener l) {
        showListener = l;
        return (T) this;
    }

    public interface OnDialogHideListener {
        void hide();
    }

    public interface OnDialogShowListener {
        void show();
    }
}

